const Service = require('egg').Service;

class AppService extends Service {

  /*
  * @author JinHang
  * @description 根据device中间件获取的userAgent来对一些平台进行初始化
  * @return {object} 包含name和options的对象，name为平台名，options为平台所需的一些参数
  * @date 2018/6/11 15:05
  * */
  async initDeviceAndOptions() {

    let name = '';

    let options = {};

    // 微信公众号平台
    if (this.ctx.device.weChat) {

      name = 'weChat';

      // 执行签名算法
      options = await this.ctx.service.weChat.sign();

    } else {

      name = 'other';
      options = {};

    }

    return {
      name,
      options
    };

  }

  /*
  * @author JinHang
  * @description 这里做用户 session的存储
  * @date 2018/8/1 17:13
  * */
  async setUserInfoToSession(result) {

    const { ctx } = this;

    if (result.data.status === 'success') {
      const { bindBeeUser, birthday, createTime, headPic, mobileNo, nickName, sex, userNo } = result.data.object;
      ctx.session.userInfo = { bindBeeUser, birthday, createTime, headPic, mobileNo, nickName, sex, userNo };
    } else {
      ctx.session.userInfo = null;
    }

  }

}

module.exports = AppService;
